package cn.lili.modules.payment.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.constant.CommonConstant;
import cn.lili.modules.kit.params.WithdrawParams;
import cn.lili.modules.payment.entity.vo.LiUserPointsWithdrawVO;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lili.modules.payment.entity.dos.LiUserPointsWithdraw;
import cn.lili.modules.payment.service.LiUserPointsWithdrawService;
import cn.lili.modules.payment.mapper.LiUserPointsWithdrawMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author asus
* @description 针对表【li_user_points_withdraw(会员积分提现记录表)】的数据库操作Service实现
* @createDate 2023-10-09 20:02:02
*/
@Service
public class LiUserPointsWithdrawServiceImpl extends ServiceImpl<LiUserPointsWithdrawMapper, LiUserPointsWithdraw> implements LiUserPointsWithdrawService{

    @Override
    public ResultMessage<Page<LiUserPointsWithdrawVO>> withdrawPage(WithdrawParams withdrawParams) {
        AuthUser currentUser = UserContext.getCurrentUser();
        if (currentUser == null) {
            return ResultUtil.error(500, "登录已过期");
        }
        LambdaQueryWrapper<LiUserPointsWithdraw> withdrawLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //只查看自己的提现记录
        if (withdrawParams.getWithdrawType() == 1 || withdrawParams.getWithdrawType() == 2) {
            withdrawLambdaQueryWrapper.eq(LiUserPointsWithdraw::getMemberId, currentUser.getExtendId());
        }
        //积分提现审核： 平台看所有，自己的看自己.
        if (withdrawParams.getWithdrawType() == 3) {
            switch (currentUser.getScene()) {
                case MEMBER -> withdrawLambdaQueryWrapper.eq(LiUserPointsWithdraw::getMemberId, currentUser.getId());
                case STORE, SUPPLIER, URBAN_SUBSTATION -> withdrawLambdaQueryWrapper.eq(LiUserPointsWithdraw::getMemberId, currentUser.getExtendId());
                default -> {
                }
            }
        }

        if (withdrawParams.getWithdrawType() != null) {
            withdrawLambdaQueryWrapper.eq(LiUserPointsWithdraw::getWithdrawType,  withdrawParams.getWithdrawType());
        }
        withdrawLambdaQueryWrapper.orderByDesc(LiUserPointsWithdraw::getCreateTime);
        Page<LiUserPointsWithdraw> pointsHistoryPage = this.page(PageUtil.initPage(withdrawParams), withdrawLambdaQueryWrapper);
        Page<LiUserPointsWithdrawVO> page = new Page<>();
        page.setSize(pointsHistoryPage.getSize());
        page.setPages(pointsHistoryPage.getPages());
        page.setCurrent(pointsHistoryPage.getCurrent());
        page.setTotal(pointsHistoryPage.getTotal());
        page.setRecords(this.conversion(pointsHistoryPage.getRecords()));
        return ResultUtil.data(page);
    }

    private List<LiUserPointsWithdrawVO> conversion(List<LiUserPointsWithdraw> records) {
        if (CollectionUtil.isEmpty(records)) {
            return new ArrayList<>();
        }
        List<LiUserPointsWithdrawVO> resultList = BeanUtil.copyToList(records, LiUserPointsWithdrawVO.class);
        for (LiUserPointsWithdrawVO liUserPointsWithdrawVO : resultList) {
            if (CommonConstant.AUDIT_STATUS_00.equals(liUserPointsWithdrawVO.getAuditStatus())) {
                liUserPointsWithdrawVO.setAuditStatusName("未审核");
            }
            if (CommonConstant.AUDIT_STATUS_01.equals(liUserPointsWithdrawVO.getAuditStatus())) {
                liUserPointsWithdrawVO.setAuditStatusName("通过审核");
            }
            if (CommonConstant.AUDIT_STATUS_02.equals(liUserPointsWithdrawVO.getAuditStatus())) {
                liUserPointsWithdrawVO.setAuditStatusName("驳回审核");
            }
        }
        return resultList;
    }
}




